/**
 * @author chenz
 */

websitenotes.noteTrashed = function(e) {
	try {
		alert(e.currentTarget.id);
		//websitenotes.noteHashArray.trashNote(uuid);
		//websitenotes.doc.body.removeChild(websitenotes.doc.getElementById("note-"+uuid));
	}
	catch (e) {
		alert(e);
	}
}

websitenotes.websiteMouseTracker = function (e) {
	var posx = 0; var posy = 0;
	if (!e) var e = window.event;
	posx = e.pageX; posy = e.pageY;
	websitenotes.posx = posx;
	websitenotes.posy = posy;
	websitenotes.mouseCoordsStatusbar.label = "("+websitenotes.posx+", "+websitenotes.posy+")";
	
	if (websitenotes.dragElement && websitenotes.dragElement.id.substring(0,4) == "note") {
		websitenotes.dragElement.style.top = ((websitenotes.posy-websitenotes.dragClickY)+websitenotes.dragStartY)+"px";
		websitenotes.dragElement.style.left = ((websitenotes.posx-websitenotes.dragClickX)+websitenotes.dragStartX)+"px";
	}
}


websitenotes.mouseUp = function(evt) {
	try {
		if (websitenotes.dragElement) {
			delete websitenotes.dragClickX;
			delete websitenotes.dragClickY;
			delete websitenotes.dragStartX;
			delete websitenotes.dragStartY;
			var uuid = websitenotes.dragElement.id.substring(5);
			var obj = websitenotes.getNoteJSONObjFromLocalDatabase(uuid).parseJSON();
    		obj.noteDiv.style.top = websitenotes.dragElement.style.top;
			obj.noteDiv.style.left = websitenotes.dragElement.style.left;
			websitenotes.setNoteJSONObjInLocalDatabase(uuid, obj.toJSONString());
			websitenotes.reloadNotes();
			delete websitenotes.dragElement;
		}
	}
	catch (e) {
		alert(e);
		delete websitenotes.dragElement;
	}
}

websitenotes.mouseDown = function(evt) {
	
	if (!evt)	evt = websitenotes.window.event;
	
	var s = "header-";

	var elem = ((evt.target) ? evt.target : evt.srcElement);
	//alert("Element ID:"+elem.id+"  Evt:"+evt);
	
	if (elem.id && elem.id.indexOf (s) == 0 /*&& elem.isWebsiteNote*/) {
		websitenotes.dragElement = elem.offsetParent;
		websitenotes.dragStartX = elem.offsetParent.offsetLeft;
		websitenotes.dragStartY = elem.offsetParent.offsetTop;
		websitenotes.dragClickX = websitenotes.posx;
		websitenotes.dragClickY = websitenotes.posy;
		//alert("1:("+websitenotes.dragStartX+", "+websitenotes.dragStartY+") 2:("+websitenotes.dragClickX+", "+websitenotes.dragClickY+")");
	}
	return;
}


function rnum(max) {
  var ranNum = Math.floor(Math.random()*max);
  return ranNum;
}